Plotting daily estimates for yall zero coupon yields for 2020
dfasxts <- as.xts(x = df[, -1], order.by = df$Date)
covid_dates <- as.xts(x = covid_dates[, -1], order.by = covid_dates$Date)
dygraph(covid_dates, main = "All Zero Coupon Yields (1-30) 2020", ylab = "Value") %>%
dyAxis('x', axisLabelFontSize = 12) %>%
dyRangeSelector()
df$Date <- as.Date(df$Date)
df$year <- format(as.Date(df$Date, format="%m/%d/%Y"),"%Y")
df <- select(df, -Date)
df <- na.omit(df)
df <- df %>%
group_by(year) %>%
summarise_all(mean)
SVENY01 <- df$SVENY01
SVENY10 <- df$SVENY10
SVENY30 <- df$SVENY30
data <- data.frame(df, SVENY01, SVENY10, SVENY30)
data <- select(data, year, SVENY01, SVENY10, SVENY30)
fig <- plot_ly(data, x = data$year, y = ~SVENY01, name = 'SVENY01', type = 'scatter', mode = 'lines + markers')
fig <- fig %>% add_trace(y = ~SVENY10, name = 'SVENY10', mode = 'lines + markers')
fig <- fig %>% add_trace(y = ~SVENY30, name = 'SVENY30', mode = 'lines + markers')
fig <- fig %>% layout(title = "",
xaxis = list(title = "year"),
yaxis = list (title = "value"))
1, 10, and 30 year maturity bond yield averages across time
fig
### Bond yield heat map
row.names(df) <- df$year
## Warning: Setting row names on a tibble is deprecated.
df <- select(df, -year)
df_matrix <- data.matrix(df)
df_heatmap <- heatmap(df_matrix, Rowv=NA, Colv=NA, col = brewer.pal(6, "Greys"), scale="column", margins=c(4,1), main = "Heatmap")

# plotting the evaluation of bond yields
library(viridisLite)
yields <- dfasxts
plot.type <- "single"
plot.palette <- magma(n = 30)
asset.names <- colnames(dfasxts)
plot.zoo(x = dfasxts, plot.type = "single", col = plot.palette, ylab = "", xlab = "")
legend(x = "topleft", legend = asset.names, col = plot.palette, cex = 0.45, lwd = 3)

dfasxts_d <- diff(dfasxts)
plot.zoo(x = dfasxts_d, plot.type = "multiple", ylim = c(-0.5, 0.5), cex.axis = 0.7, ylab = 1:30, col = plot.palette, main = "", xlab = "")

dfasxts <- dfasxts_d["2000/",]
x_1 <- dfasxts[,"SVENY01"]
x_20 <- dfasxts[, "SVENY20"]
# Plot the autocorrelations of the yield changes)
par(mar=c(5.1, 4.1, 4.1, 2.1))
par(mfrow=c(2,2))
acf_1 <- acf(x_1)
acf_20 <- acf(x_20)
# Plot the autocorrelations of the absolute changes of yields
acf_abs_1 <- acf(abs(x_1))
acf_abs_20 <- acf(abs(x_20))

spec <- ugarchspec(distribution.model = "sstd")
fit_1 <- ugarchfit(x_1, spec = spec)
vol_1 <- sigma(fit_1)
res_1 <- scale(residuals(fit_1, standardize = TRUE)) * sd(x_1) + mean(x_1)
merge_1 <- merge.xts(x_1, vol_1, res_1)
plot.zoo(merge_1, xlab = "Year")

####################
fit_20 <- ugarchfit(x_20, spec = spec)
vol_20 <- sigma(fit_20)
res_20 <- scale(residuals(fit_20, standardize = TRUE)) * sd(x_20) + mean(x_20)
merge_20 <- merge.xts(x_20, vol_20, res_20)
plot.zoo(merge_20, xlab = "Year")

par(mar=c(5.1, 4.1, 4.1, 2.1))
par(mfrow=c(2,1))
hist(res_1)
hist(res_20)

ugarchspec()
##
## *---------------------------------*
## * GARCH Model Spec *
## *---------------------------------*
##
## Conditional Variance Dynamics
## ------------------------------------
## GARCH Model : sGARCH(1,1)
## Variance Targeting : FALSE
##
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model : ARFIMA(1,0,1)
## Include Mean : TRUE
## GARCH-in-Mean : FALSE
##
## Conditional Distribution
## ------------------------------------
## Distribution : norm
## Includes Skew : FALSE
## Includes Shape : FALSE
## Includes Lambda : FALSE
density_x_1 <- density(x_1)
density_res_1 <- density(res_1)
plot(density_x_1)
lines(density_res_1, col = "red")
norm_dist <- dnorm(seq(-0.4, 0.4, by = .01), mean = mean(x_1), sd = sd(x_1))
lines(seq(-0.4, 0.4, by = .01),
norm_dist,
col = "darkblue"
)
# Add legend
legend <- c("Before GARCH", "After GARCH", "Normal distribution")
legend("topleft", legend = legend,
col = c("black", "red", "darkblue"), lty=c(1,1))

distribution <- qnorm
qqnorm(x_1, ylim = c(-0.5, 0.5))
qqline(x_1, distribution = distribution, col = "darkgreen")
par(new=TRUE)
qqnorm(res_1 * 0.614256270265139, col = "red", ylim = c(-0.5, 0.5))
qqline(res_1 * 0.614256270265139, distribution = distribution, col = "darkgreen")
legend("topleft", c("Before GARCH", "After GARCH"), col = c("black", "red"), pch=c(1,1))
